package com.zj.osm.view.home.home.home_fragment.threefragment

import android.view.View
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.flyco.dialog.listener.OnBtnClickL
import com.flyco.dialog.widget.NormalDialog
import com.zj.osm.App
import com.zj.osm.R
import com.zj.osm.view.BaseFragment
import com.zj.osm.view.account.UserInformationActivity
import com.zj.osm.view.account.login.LoginActivity
import com.zj.osm.view.mine.AboutActivity
import com.zj.osm.view.mine.changedb.ChangeDBActivity
import com.zj.utils.view.dialog.NetworkUtil
import kotlinx.android.synthetic.main.three_fragment_lt.*
import zj.it.bhne.design.activity.my.DownActivity

/**
 * @project OsmDroid
 * @author jiang zhu
 * @data 2019/12/5
 */
class ThreeFragment : BaseFragment(), View.OnClickListener {

    override fun getContentViewLayoutID(): Int {
        return R.layout.three_fragment_lt
    }

    override fun initListener() {
        initView()
        changeDb.setOnClickListener(this)
        three_btn_down.setOnClickListener(this)
        three_btn_inter.setOnClickListener(this)
        three_tv_name.setOnClickListener(this)
        three_btn_more.setOnClickListener(this)
        three_btn_above.setOnClickListener(this)
        three_btn_back.setOnClickListener(this)
        three_btn_exit.setOnClickListener(this)
        three_rl_account.setOnClickListener(this)
    }

    private fun initView() {
        threeTitleBar.setTitle("我的")
        threeVersion.text = "版本v" + AppUtils.getAppVersionName()
        isLogin()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.three_tv_name -> if (App.getInstances().IsLogin()) {
                ToastUtils.showShort("已处于在线状态")
            } else {
                startIntent(LoginActivity::class.java)
                //getActivity().finish();
            }
            R.id.three_btn_more -> NetworkUtil().showSetNetHttpDialog(context!!)
            R.id.three_btn_above -> startIntent(AboutActivity::class.java)
            R.id.three_btn_back -> backDialog()
            R.id.three_btn_inter -> {
                //chinaEnglish();
                //版本更新
                ToastUtils.showShort("当前为最新版本:" + AppUtils.getAppVersionName())
                /*if (NetworkUtils.isConnected()) {
                   mProgressDialog = new ProgressDialog(getContext());
                   mProgressDialog.setMessage("正在检查更新,请稍等...");
                   mProgressDialog.setCanceledOnTouchOutside(false);
                   mProgressDialog.show();
                   ThreadManager.ThreadPool threadPool = getThreadPool();
                   threadPool.execute(mRunnable);
               } else {
                   ToastUtils.showShort("网络无连接");
               }*/
            }
            R.id.changeDb -> startIntent(ChangeDBActivity::class.java)
            R.id.three_btn_exit -> {

                val dialog = NormalDialog(context)
                dialog.content("是否要退出程序?")//
                        .style(NormalDialog.STYLE_TWO)//
                        .btnNum(2)
                        .title("退出")
                        .btnText("取消", "退出")//
                        .show()
                dialog.setOnBtnClickL(OnBtnClickL { dialog.dismiss() }, OnBtnClickL {
                    dialog.dismiss()
                    activity?.finish()
                    SPUtils.getInstance().put("update", "0")
                    App.getInstances().exit(1)
                })


            }
            //R.id.three_rl_account -> startActivity(Intent(context, AccountActivity::class.java))
            R.id.ft_header_iv -> startIntent(UserInformationActivity::class.java)
            R.id.three_btn_down -> startIntent(DownActivity::class.java)
        }
    }

    /**
     * 退出登录
     */
    private fun backDialog() {
        val dialog = NormalDialog(context)
        dialog.content("是否要退出登录?")//
                .style(NormalDialog.STYLE_TWO)//
                .btnNum(2)
                .title("退出")
                .btnText("取消", "退出")//
                .show()
        dialog.setOnBtnClickL(OnBtnClickL { dialog.dismiss() }, OnBtnClickL {
            dialog.dismiss()
            activity?.finish()
            SPUtils.getInstance().put("islogin", "0")
            SPUtils.getInstance().put("mUsername", "")
            SPUtils.getInstance().put("mPassword", "")
            SPUtils.getInstance().put("token", "")
            SPUtils.getInstance().put("name", "")
            //startActivity(Intent(activity, LoginActivity::class.java))
        })

    }

    /**
     * 判断是否登录
     */
    private fun isLogin() {
        if (App.getInstances().IsLogin()) {
            three_btn_back.visibility = View.VISIBLE
            three_rl_account.visibility = View.VISIBLE
            val names = SPUtils.getInstance().getString("name")
            if (names != "") {
                three_tv_name.text = names
            }
        } else {
            three_btn_back.visibility = View.GONE
            three_rl_account.visibility = View.GONE
        }
    }

}